package com.patchmanage.back.controller;

import com.patchmanage.back.entity.Inventory;
import com.patchmanage.back.result.CodeMsg;
import com.patchmanage.back.result.Result;
import com.patchmanage.back.service.IDepartmentGroupService;
import com.patchmanage.back.service.IExcelService;
import com.patchmanage.back.service.IInventoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * <p>
 * 数据导出导入 控制器
 * </p>
 *
 * @author huizhao
 * @since 2021-03-20
 */
@Api(value = " 数据导出导入 controller", tags = {"数据导出导入 Api"})
@RestController
@RequestMapping("/back/excel")
public class ExcelController {

    @Autowired
    public IInventoryService inventoryService;

    @Autowired
    public IDepartmentGroupService departmentGroupService;

    @Autowired
    public IExcelService excelService;

    /**
     * 导出资产列表接口
     *
     * @param inventory
     * @return
     */
    @ApiOperation(value = "导出资产列表接口", notes = "获取资产列表接口")
    @RequestMapping(value = "exportInventorylist", method = RequestMethod.GET)
    public Result<Boolean> exportInventorylist(HttpServletResponse response, Inventory inventory) {
        return  Result.success(excelService.exportInventory(response, inventory));
    }

    /**
     * 导入资产
     *
     * @param file
     * @return
     */
    @ApiOperation(value = "导入资产列表接口", notes = "获取资产列表接口")
    @RequestMapping(value = "importInventorylist", method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    public Result<Boolean> importInventorylist(@RequestParam("file") MultipartFile file) throws IOException {
        //存入服务器
        String filePathDirectory = "/usr/local/patch-manage-back/file/";
        File newFile = new File(filePathDirectory + file.getOriginalFilename());
        file.transferTo(newFile);
        excelService.importInvertory(newFile);
        return  Result.success(true);
    }

    /**
     * 下载资产模板文件
     * @return
     */
    @ApiOperation(value = "下载资产模板文件", notes = "下载资产模板文件")
    @RequestMapping(value = "downloadInventoryDemo", method = RequestMethod.GET)
    public Result<Object> downloadInventoryDemo(HttpServletResponse response) {
        //设置文件路径
        File file = new File("/usr/local/patch-manage-back/file/inventory.xlsx");
        if (file.exists()) {
            response.setContentType("application/force-download");// 设置强制下载不打开
            response.addHeader("Content-Disposition", "attachment;fileName=" + file.getName());// 设置文件名
            byte[] buffer = new byte[1024];
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            try {
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                OutputStream os = response.getOutputStream();
                int i = bis.read(buffer);
                while (i != -1) {
                    os.write(buffer, 0, i);
                    i = bis.read(buffer);
                }
                Result.success("下载成功");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (bis != null) {
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return Result.error(CodeMsg.SERVER_ERROR);
    }

}
